Java Collection Framework Quiz

Easy Level

1. Which of these is the primary interface that all collection classes in the Java Collection Framework implement?

2. What is the defining characteristic of a class that implements the `Set` interface?

3. Which collection class provides an ordered collection (also called a sequence) and allows duplicate elements?

4. The `Map` interface is used to store what kind of data?

5. Which implementation of `List` is most efficient for retrieving an element by its index?

6. To add a new element to a `List` or a `Set`, which method is typically used?

7. Which `Set` implementation stores its elements in a hash table and does not guarantee any order of iteration?

8. How do you retrieve a value from a `Map` if you know its corresponding key?

9. Which interface represents a collection that orders elements in a FIFO (First-In, First-Out) manner?

10. What does the `size()` method of a collection return?

Medium Level

11. For which of the following operations is `LinkedList` generally more efficient than `ArrayList`?

12. What is the main difference between a `HashSet` and a `TreeSet`?

13. If you want to store key-value pairs and iterate through them in the sorted order of their keys, which `Map` implementation should you use?

14. What happens if you try to add a duplicate element to a `HashSet`?

15. Which two methods must be correctly implemented for a custom object to be used as a key in a `HashMap`?

16. Which collection class maintains the insertion order of elements?

17. What is a `Deque`?

18. The `java.util.Collections` class contains:

19. Which of the following is true about the keys in any `Map` implementation?

20. If you iterate over a `LinkedHashMap`, in what order will you retrieve its entries?

Hard Level

21. What is the primary underlying data structure for a `HashMap` in Java 8 and later?

22. Which collection is considered thread-safe and is highly optimized for concurrent read operations?

23. Under what condition is an entry removed from a `WeakHashMap`?

24. What is the main performance advantage of `ArrayDeque` over `LinkedList` when used as a `Stack` or `Queue`?

25. How does an `IdentityHashMap` differ from a regular `HashMap` when checking for key equality?

26. What is a key feature of the `BlockingQueue` interface?

27. The `NavigableSet` interface provides methods like `floor()`, `ceiling()`, `higher()`, and `lower()`. What do these methods do?

28. Why is `EnumSet` a highly efficient `Set` implementation for `enum` types?

29. What is a key performance difference between `ConcurrentHashMap` and a map created with `Collections.synchronizedMap()`?

30. What happens if you add an object to a `TreeSet` that does not implement the `Comparable` interface (and no `Comparator` is provided)?